package com.smart.wap.service;

import com.alibaba.fastjson2.JSONObject;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.manager.member.entities.models.MemberProfileMain;
import com.smart.manager.pop.entities.models.PopActivityMain;
import com.smart.manager.pop.repository.ActivityMainRepository;
import com.smart.manager.pop.repository.ActivityRecordRepository;
import com.smart.manager.pop.repository.DisRecordRepository;
import com.smart.manager.system.repository.ConfigRepository;
import jodd.util.ThreadUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @author Administrator
 */
@Service
@Log4j2
@RequiredArgsConstructor
public class BindInviterService {
    private final ActivityMainRepository activityMainRepository;
    private final ActivityRecordRepository achievedRepository;
    private final ConfigRepository configRepository;
    private final DisRecordRepository disRecordRepository;

    @Async
    public void bingInviter(MemberProfileMain newMember, String inviteMode, long inviterId) {
        ThreadUtil.sleep(500);
        switch (inviteMode) {

            case "C2C":
                bindC2c(newMember, inviterId);
                break;
            case "B2C":
                bindB2c(newMember, inviterId);
                break;
            default:
                log.warn("无法识别的邀请方式：" + inviteMode);
                break;
        }
    }


    /**
     * 绑定C2C
     */
    public void bindC2c(MemberProfileMain newMember, long inviterId) {

        JSONObject jsonObject = configRepository.readCache(SmartConstant.SHARE_CONFIG);
        long activityId = jsonObject.getLongValue("ActivityC2C");
        PopActivityMain popActivityMain = activityMainRepository.readOne(activityId);
        newMember.setInviteMode("C2C").setInviterId(inviterId).save();
        Long actorId = newMember.getId();
        // 如果存在真实的邀请人信息 那么进入邀请人数据结算程序
        achievedRepository.saveRecord(popActivityMain, inviterId, actorId);

    }

    /**
     * 绑定B2C
     */
    public void bindB2c(MemberProfileMain newMember, long inviterId) {
        newMember.setInviteMode("B2C").setInviterId(inviterId).update();
        disRecordRepository.saveRecord(newMember, inviterId);
    }
}

